<?php
/**
 * 系統名稱: Lotto 系統
 * 檔案說明: 專案管理-修改
 * $Author$
 * $Id$
 *
 */
/*=================================================================*/
header("Cache-Control: no-store, no-cache, must-revalidate");	  // HTTP/1.1
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");															  // HTTP/1.0
?><meta http-equiv="Content-Type" content="text/html; charset=utf-8 print $_Charset;?>"><?php
if($arrResult["WagerCon"] != ""){
	$arrResult["WagerCon"] = str_replace(chr(10), "", $arrResult["WagerTip"]."<br />".$arrResult["WagerCon"]);
	$arrResult["WagerCon"] = str_replace(chr(13), "", $arrResult["WagerCon"]);
	print "<script language=\"JavaScript\">\n<!--\n";
	print "parent.SuppleResult.innerHTML='".addslashes($arrResult["WagerCon"])."';\n";
	print "parent.open_SuppleResultWin();\n";
	print "//-->\n</script>\n";
	ob_end_flush();
	flush();
}

$isSpecial = intval($_data["isSpecial"]);
$isRealShow = intval($_data["isRealShow"]);
$intGID = intval($_data["GambleID"]);
if(strlen($_data["isSpecial"]) == 0)$isSpecial = 1;
if(strlen($_data["isRealShow"]) != 1)$isRealShow = 1;
$intSpecialNumType = intval($_data["SpecialNumType"]);
$strBT = trim($_data["Bet_Type"]);
$arrBType = explode(",", $strBT);
$arrOtherMsg = explode(",", trim($_data["OtherMsg"]));
$strLottoNumber = trim($_data["strLottoNumber"]);

switch($arrPower[1]){
	case "all":
		$strSeach = " ";
		$strMulti = ($isRealShow == 1)?"*(DirectorRate)":"";
		$strComm = ($isRealShow == 1)?"*DirectorCommission":"*0";
		$intSWType = 1;
		break;

	case "director":
		if($isRealShow == 2 && $_SESSION["Admin_Info"] -> isAllAccount == 1){	//公司總帳
			$strSeach = " ";
			$strMulti = "*(DirectorRate)";
			$strComm = "*(DirectorCommission)";
		}else{
			$strSeach = " and DirectorID = ".$_SESSION["Admin_Info"] -> ID." ";
			$strMulti = ($isRealShow == 1)?"*(StockRate - DirectorRate)":"";
			$strComm = ($isRealShow == 1)?"*StockCommission":"*0";
		}
		$intSWType = 5;
		break;

	case "stock":
		/*if($isRealShow == 2 && $_SESSION["Admin_Info"] -> isAllAccount == 1){	//公司總帳
			$strSeach = " and DirectorID = ".$_SESSION["Admin_Info"] -> ID." ";
			$strMulti = "*(DirectorRate)";
			$strComm = "*(DirectorCommission)";
		}else{*/
			$strSeach = " and StockID = ".$_SESSION["Admin_Info"] -> ID." ";
			$strMulti = ($isRealShow == 1)?"*(MainRate - StockRate)":"";
			$strComm = ($isRealShow == 1)?"*MainCommission":"*0";
		//}
		$intSWType = 4;
		break;

	case "main":
		$strSeach = " and MainID = ".$_SESSION["Admin_Info"] -> ID." ";
		$strMulti = ($isRealShow == 1)?"*(SubRate - MainRate)":"";
		$strComm = ($isRealShow == 1)?"*SubCommission":"*0";
		$intSWType = 3;
		break;

	case "sub";
		$strSeach = " and SubID = ".$_SESSION["Admin_Info"] -> ID." ";
		$strMulti = ($isRealShow == 1)?"*(1 - SubRate)":"";
		$strComm = ($isRealShow == 1)?"*MemCommission":"*0";
		$intSWType = 2;
		break;
}

$arrSumStake = array();
$arrSumPayOff = array();
$arrSumCommission = array();
$arrSumSuppleStake = array();
$arrSumSupplePayOff = array();
$arrSumSuppleCommission = array();
$arr92Sort = array(7, 8, 9, 10, 11, 13);
$intBT = (int)$strBT;

if(in_array($intBT, $arr92Sort)){ //三定位
	$strMulti = str_replace('_', '', $strMulti);
	$strComm = str_replace('_', '', $strComm);
	$strSumSQL = "select WagerType, Count(*) as total, sum(IF(WagerType = ".$intSWType.",MemStake,MemStake".$strMulti.")*MemExchangeRate) as betstatus, sum(IF(WagerType = ".$intSWType.",MemPayOff,MemPayOff".$strMulti.")*MemExchangeRate) as paystatus,  sum(IF(WagerType = ".$intSWType.",MemStake,MemStake".$strMulti.")*MemExchangeRate".$strComm.") as commstatus from `bet_BetListTmp` where Bet_Type in (".implode(",", $arrBType).") and GambleID = ".$intGID.$strSeach." and isPayOff >= 0 and Bet_Type = '".$strBT."' group by WagerType";

	if(!$RS = PMA_mysql_query($strSumSQL, $$_Conn)){
		$GLOBALS["errormessage"] = "Find TodayGame Data Error!!";
		$GLOBALS["errorhidden"] = "Error: ".PMA_mysql_error($$_Conn)."\nSQL:".$strSQL."\nLine:".__LINE__."\nPage:".__FILE__;
		ErrMsgOutputHTML("Error", "", 1);
	}
	$arrSumStake[$intBT] = 0;
	$arrSumPayOff[$intBT] = 0;
	$arrSumCommission[$intBT] = 0;
	$arrSumSuppleStake[$intBT] = 0;
	$arrSumSupplePayOff[$intBT] = 0;
	$arrSumSuppleCommission[$intBT] = 0;
	while($Rows = PMA_mysql_fetch_object($RS)){
		if($Rows -> WagerType == $intSWType){
			$arrSumSuppleStake[$intBT] = $Rows -> betstatus;
			$arrSumSupplePayOff[$intBT] = $Rows -> paystatus;
			$arrSumSuppleCommission[$intBT] = $Rows -> commstatus;
		}else{
			$arrSumStake[$intBT] = $Rows -> betstatus;
			$arrSumPayOff[$intBT] = $Rows -> paystatus;
			$arrSumCommission[$intBT] = $Rows -> commstatus;
		}
	}

	$strSQL = "select Bet_Type, Number, sum(IF(WagerType = ".$intSWType.",0,1)) as total, sum(IF(WagerType = ".$intSWType.",1,0)) as total_supple, sum(IF(WagerType = ".$intSWType.",0,MemStake".$strMulti.")*MemExchangeRate) as betstatus, sum(IF(WagerType = ".$intSWType.",0,MemPayOff".$strMulti.")*MemExchangeRate) as paystatus,  sum(IF(WagerType = ".$intSWType.",0,MemStake".$strMulti.")*MemExchangeRate".$strComm.") as commstatus, sum(IF(WagerType = ".$intSWType.",MemStake,0)*MemExchangeRate) as betstatus_supple, sum(IF(WagerType = ".$intSWType.",MemPayOff,0)*MemExchangeRate) as paystatus_supple,  sum(IF(WagerType = ".$intSWType.",MemStake,0)*MemExchangeRate".$strComm.") as commstatus_supple, (".($arrSumStake[$intBT] - $arrSumCommission[$intBT] - $arrSumSuppleStake[$intBT] + $arrSumSuppleCommission[$intBT])." - sum(IF(WagerType = ".$intSWType.",MemStake*-1,MemStake".$strMulti.")*MemExchangeRate) - sum(IF(WagerType = ".$intSWType.",MemPayOff*-1,MemPayOff".$strMulti.")*MemExchangeRate)) as preWin from `bet_BetListTmp` where Bet_Type in (".join(",", $arrBType).") and GambleID = ".$intGID.$strSeach." and isPayOff >= 0 ";
	switch($arrOtherMsg[0]){
		case "Sort": //排序
			$strSQL .= "group by Bet_Type, Number order by preWin, Number limit ";
			$strSQL .= ($strBT != "13")?"100":"220";
			break;

		default:
			$strSQL .= "and Number in (".$strLottoNumber.") group by Bet_Type, Number, WagerType order by Number";
			break;
	}
}else{
	$strSQL = "select Bet_Type, Number, sum(IF(WagerType = ".$intSWType.",0,1)) as total, sum(IF(WagerType = ".$intSWType.",1,0)) as total_supple, sum(IF(WagerType = ".$intSWType.",0,MemStake".$strMulti.")*MemExchangeRate) as betstatus, sum(IF(WagerType = ".$intSWType.",0,MemPayOff".$strMulti.")*MemExchangeRate) as paystatus,  sum(IF(WagerType = ".$intSWType.",0,MemStake".$strMulti.")*MemExchangeRate".$strComm.") as commstatus, sum(IF(WagerType = ".$intSWType.",MemStake,0)*MemExchangeRate) as betstatus_supple, sum(IF(WagerType = ".$intSWType.",MemPayOff,0)*MemExchangeRate) as paystatus_supple,  sum(IF(WagerType = ".$intSWType.",MemStake,0)*MemExchangeRate".$strComm.") as commstatus_supple from `bet_BetListTmp` where Bet_Type in (".join(",", $arrBType).") and GambleID = ".$intGID.$strSeach." and isPayOff >= 0 group by Bet_Type, Number";
}
//print $strSQL;
if(!$RS = PMA_mysql_query($strSQL, $$_Conn)){
	$GLOBALS["errormessage"] = "Find Wager Data Error!!";
	$GLOBALS["errorhidden"] = "Error: ".PMA_mysql_error($$_Conn)."\nSQL:".$strSQL."\nLine:".__LINE__."\nPage:".__FILE__;
	ErrMsgOutputHTML("Error", "", 1);
}

$arrNum92Sort = array();
$arrNum = array();
$arrExNum = array();
while($Rows = PMA_mysql_fetch_object($RS)){
	if($arrOtherMsg[0] == "Sort"){
		if($Rows -> preWin > 0){
			$arrExNum[] = $Rows -> Number;
			continue;
		}
	}
	$intBT = $Rows -> Bet_Type;

	$arrSpWCount[$intBT][$Rows -> Number] += $Rows -> total_supple;
	$arrSpWSum[$intBT][$Rows -> Number] += $Rows -> betstatus_supple;
	$arrSpPayOff[$intBT][$Rows -> Number] += $Rows -> paystatus_supple;
	$arrSpCommission[$intBT][$Rows -> Number] += $Rows -> commstatus_supple;

	$arrWCount[$intBT][$Rows -> Number] += $Rows -> total;
	$arrWSum[$intBT][$Rows -> Number] += $Rows -> betstatus;
	$arrPayOff[$intBT][$Rows -> Number] += $Rows -> paystatus;
	$arrCommission[$intBT][$Rows -> Number] += $Rows -> commstatus;

	$arrNum92Sort[$Rows -> Number] = true;
	$arrNum[] = $Rows -> Number;
}


if(in_array($intBT, $arr92Sort)){
	if($arrOtherMsg[0] == "Sort"){ //三定位
		if($intBT != 13){
			$i = 0;
			while(sizeof($arrNum92Sort) < 100){
				if(!$arrNum92Sort[$i] && !in_array($i, $arrExNum)){
					$arrNum92Sort[$i] = true;
					$arrNum[] = $i;
				}
				$i++;
			}
			$arrTodayGame = getTodayGameDataForMultiNumber($intLottoID, $intGID, $intBT, join(",", $arrNum));
		}else{
			$arrLottoNumber = explode(",", $strLottoNumber);
			foreach($arrLottoNumber as $intLNum){
				if(!$arrNum92Sort[$intLNum]){
					$arrNum92Sort[$intLNum] = true;
				}
			}
			$arrTodayGame = getTodayGameDataForMultiNumber($intLottoID, $intGID, $intBT, $strLottoNumber);
		}
	}else{
		$arrTodayGame = getTodayGameDataForMultiNumber($intLottoID, $intGID, $intBT, $strLottoNumber);
	}
	if($arrTodayGame === false)ErrMsgOutputHTML("Error", "", 1);
}else{
	for($i = 0; $i < sizeof($arrBType); $i++)include(__Common_Path."/todaygame/TodayGame".$intLottoID."_".$arrBType[$i].".inc.php");
	$arrTodayGame = $_arrTodayGame[$intGID];
}

if(!is_array($arrTodayGame)){
	$GLOBALS["errormessage"] = "No TodayGame Data!!";
	ErrMsgOutputHTML("Error", "", 1);
}

$intTmpBT = 0;
$intNo = -1;
foreach($arrTodayGame as $intBT => $arrN){
	$arrTmpNum = (in_array($intBT, $arr92Sort) && $arrOtherMsg[0] == "Sort")?$arrNum92Sort:$arrN;

	foreach($arrTmpNum as $intNum => $v){
		$Rows = $arrTodayGame[$intBT][$intNum];
		if(in_array($intBT, $arr92Sort) && $arrOtherMsg[0] != "Sort" && !preg_match("/,".$Rows["Number"].",/", ",".$strLottoNumber.","))continue;

		$intWCount = $arrWCount[$intBT][$intNum]+0;
		$tolStake = $arrWSum[$intBT][$intNum]+0;
		$tolPayOff = $arrPayOff[$intBT][$intNum]+0;
		$tolCommission = $arrCommission[$intBT][$intNum]+0;
		//補貨
		$intSpWCount = $arrSpWCount[$intBT][$intNum]+0;
		$tolSuppleStake = $arrSpWSum[$intBT][$intNum]+0;
		$tolSupplePayOff = $arrSpPayOff[$intBT][$intNum]+0;
		$tolSuppleCommission = $arrSpCommission[$intBT][$intNum]+0;

		$intApointNum = $_arrGLineNumToApointNum[$Rows["LottoID"]][$Rows["Bet_Type"]][$Rows["Number"]];
		if($intApointNum == null)$intApointNum = 0;
		$flGLine = $_arrGambleType[$Rows["Bet_Type"]][$intApointNum];

		$intNo = (in_array((int)$Rows["Bet_Type"], $arr92Sort))?($intNo+1):$Rows["Number"];

		$strCon .= "parent.Game".$intBT."[".$intNo."]=new Array('".$Rows["LottoID"]."','".$intGID."','".$Rows["Bet_Type"]."','".$Rows["Number"]."','".$Rows["OddsID"]."','".($Rows["Odds"]+$flGLine)."','".$Rows["isStopBet"]."','".$intWCount."','".$tolStake."','".$tolPayOff."','".$tolCommission."','".$intSpWCount."','".$tolSuppleStake."','".$tolSupplePayOff."','".$tolSuppleCommission."');\n";

		switch($intBT){
			case 7:
			case 8:
			case 9:
			case 10:
			case 11:
			case 13:
				break;

			case 14:
			case 15:
			case 16:
			case 17:
				$intTmp = floor($Rows["Number"] / 10);
				$arrSumStake[$Rows["Bet_Type"]][$intTmp] += $tolStake;
				$arrSumPayOff[$Rows["Bet_Type"]][$intTmp] += $tolPayOff;
				$arrSumCommission[$Rows["Bet_Type"]][$intTmp] += $tolCommission;
				$arrSumSuppleStake[$Rows["Bet_Type"]][$intTmp] += $tolSuppleStake;
				$arrSumSupplePayOff[$Rows["Bet_Type"]][$intTmp] += $tolSupplePayOff;
				$arrSumSuppleCommission[$Rows["Bet_Type"]][$intTmp] += $tolSuppleCommission;
				break;

			default:
				$arrSumStake[$Rows["Bet_Type"]] += $tolStake;
				$arrSumPayOff[$Rows["Bet_Type"]] += $tolPayOff;
				$arrSumCommission[$Rows["Bet_Type"]] += $tolCommission;
				$arrSumSuppleStake[$Rows["Bet_Type"]] += $tolSuppleStake;
				$arrSumSupplePayOff[$Rows["Bet_Type"]] += $tolSupplePayOff;
				$arrSumSuppleCommission[$Rows["Bet_Type"]] += $tolSuppleCommission;
				break;
		} //switch($intBT)

	} //foreach($arrTmpNum as $intNum => $v)
} //foreach($arrTodayGame as $intBT => $arrN)

/*=================================================================*/
//header("Cache-Control: no-store, no-cache, must-revalidate");	  // HTTP/1.1
//header("Cache-Control: post-check=0, pre-check=0", false);
//header("Pragma: no-cache");															  // HTTP/1.0
?>
<script language="JavaScript">
<!--
<?php 
print $strCon."\n";

foreach($arrSumStake as $intBT => $val){
	if(is_array($val)){
		foreach($val as $intN => $val2){
			print "parent.sumStake[".$intBT."][".$intN."] = ".round($val2).";\n";
			print "parent.sumPayOff[".$intBT."][".$intN."] = ".round($arrSumPayOff[$intBT][$intN]).";\n";
			print "parent.sumCommission[".$intBT."][".$intN."] = ".round($arrSumCommission[$intBT][$intN]).";\n";
			print "parent.sumSuppleStake[".$intBT."][".$intN."] = ".round($arrSumSuppleStake[$intBT][$intN]).";\n";
			print "parent.sumSupplePayOff[".$intBT."][".$intN."] = ".round($arrSumSupplePayOff[$intBT][$intN]).";\n";
			print "parent.sumSuppleCommission[".$intBT."][".$intN."] = ".round($arrSumSuppleCommission[$intBT][$intN]).";\n";
		}
	}else{
		print "parent.sumStake[".$intBT."] = ".round($arrSumStake[$intBT]).";\n";
		print "parent.sumPayOff[".$intBT."] = ".round($arrSumPayOff[$intBT]).";\n";
		print "parent.sumCommission[".$intBT."] = ".round($arrSumCommission[$intBT]).";\n";
		print "parent.sumSuppleStake[".$intBT."] = ".round($arrSumSuppleStake[$intBT]).";\n";
		print "parent.sumSupplePayOff[".$intBT."] = ".round($arrSumSupplePayOff[$intBT]).";\n";
		print "parent.sumSuppleCommission[".$intBT."] = ".round($arrSumSuppleCommission[$intBT]).";\n";
	}
}
switch($strBT){
	//定位
	case "14":
	case "15":
	case "18":
	case "19":
	case "20":
	case "21":
	case "22":
	case "14,15,18,19,20,21,22":
		print "parent.ShowNumberOdds18(18);\n";
		break;

	case "16":
	case "17":
	case "16,17":
		print "parent.ShowNumberOdds16(16);\n";
		break;

	//雙飛
	case "64";
	case "65";
	case "66";
	case "67";
	case "64,65,66";
		print "parent.ShowTwinNumer(".$strBT.");\n";
		break;

	//二定位
	case "1":
	case "2":
	case "3":
	case "4":
	case "5":
	case "6":
	case "12":
	case "1,2,3,4,5,6":
		print "parent.ShowTwinNumer(".$strBT.");\n";
		break;

	case "7":
	case "8":
	case "9":
	case "10":
		print "parent.ShowTrinityNumber(".$strBT.");\n";
		break;

	case "11":
		print "parent.ShowFourNumber(11);\n";
		break;

	case "13":
		print "parent.Show3WordNumber(13);\n";
		break;
}

/*if($arrResult["WagerCon"] != ""){
	$arrResult["WagerCon"] = str_replace(chr(10), "", $arrResult["WagerTip"]."<br />".$arrResult["WagerCon"]);
	$arrResult["WagerCon"] = str_replace(chr(13), "", $arrResult["WagerCon"]);
	print "parent.SuppleResult.innerHTML='".addslashes($arrResult["WagerCon"])."';\n";
	print "parent.open_SuppleResultWin();\n";
}*/
?>
//-->
</script>